package com.ruoyi.archive.service.impl;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.archive.domain.SDemobilizationSoldier;
import com.ruoyi.archive.domain.SSoldierBaseInfo;
import com.ruoyi.archive.service.ISTransferredCadresService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.archive.mapper.SDisabledSoldierMapper;
import com.ruoyi.archive.domain.SDisabledSoldier;
import com.ruoyi.archive.service.ISDisabledSoldierService;

/**
 * 残疾军人信息Service业务层处理
 *
 * @author Allen
 * @date 2021-09-30
 */
@Service
public class SDisabledSoldierServiceImpl extends ServiceImpl<SDisabledSoldierMapper, SDisabledSoldier> implements ISDisabledSoldierService {
    @Autowired
    private SDisabledSoldierMapper sDisabledSoldierMapper;

    @Autowired
    private ISTransferredCadresService transferredCadresService;

    /**
     * 查询残疾军人信息
     *
     * @param id
     *         残疾军人信息主键
     *
     * @return 残疾军人信息
     */
    @Override
    public SDisabledSoldier selectSDisabledSoldierById(Long id) {
        return sDisabledSoldierMapper.selectSDisabledSoldierById(id);
    }

    /**
     * 查询残疾军人信息列表
     *
     * @param sDisabledSoldier
     *         残疾军人信息
     *
     * @return 残疾军人信息
     */
    @Override
    public List<SDisabledSoldier> selectSDisabledSoldierList(SDisabledSoldier sDisabledSoldier) {
        List<SDisabledSoldier> disabledSoldierList = sDisabledSoldierMapper.selectSDisabledSoldierList(sDisabledSoldier);
        if (CollectionUtil.isNotEmpty(disabledSoldierList)) {
            List<Long> soldierIdList = disabledSoldierList.stream().map(SDisabledSoldier::getSoldierId).collect(Collectors.toList());
            Map<Long, SSoldierBaseInfo> baseInfoMap = transferredCadresService.getSoldierBaseInfoMap(soldierIdList);
            disabledSoldierList.stream().forEach(x -> x.setSoldierName(baseInfoMap.get(x.getSoldierId()).getName()));
            disabledSoldierList.stream().forEach(x -> x.setIdCard(baseInfoMap.get(x.getSoldierId()).getIdCard()));
        }
        return disabledSoldierList;
    }

    /**
     * 新增残疾军人信息
     *
     * @param sDisabledSoldier
     *         残疾军人信息
     *
     * @return 结果
     */
    @Override
    public int insertSDisabledSoldier(SDisabledSoldier sDisabledSoldier) {
        if (sDisabledSoldier != null) {
            return sDisabledSoldierMapper.insertSDisabledSoldier(sDisabledSoldier);
        }
        return 0;
    }

    /**
     * 修改残疾军人信息
     *
     * @param sDisabledSoldier
     *         残疾军人信息
     *
     * @return 结果
     */
    @Override
    public int updateSDisabledSoldier(SDisabledSoldier sDisabledSoldier) {
        return sDisabledSoldierMapper.updateSDisabledSoldier(sDisabledSoldier);
    }

    /**
     * 批量删除残疾军人信息
     *
     * @param ids
     *         需要删除的残疾军人信息主键
     *
     * @return 结果
     */
    @Override
    public int deleteSDisabledSoldierByIds(Long[] ids) {
        return sDisabledSoldierMapper.deleteSDisabledSoldierByIds(ids);
    }

    /**
     * 删除残疾军人信息信息
     *
     * @param id
     *         残疾军人信息主键
     *
     * @return 结果
     */
    @Override
    public int deleteSDisabledSoldierById(Long id) {
        return sDisabledSoldierMapper.deleteSDisabledSoldierById(id);
    }
}
